/*
 * Created by JFormDesigner on Tue Oct 24 08:12:06 CST 2023
 */

package cn.edu.cdp.gui.panel;

import cn.edu.cdp.service.AdminService;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author fanyi
 * （1）	查询XXX同学各科成绩：学号，姓名，专业名称，课程名称，成绩        5
 * （2）	按专业统计学生人数：专业，学生人数                           2
 * （3）	查询某门课程成绩单：学号，姓名，专业名称，成绩                 4
 * （4）	查询学生总学分，不及格不计学分：学号，姓名，专业名称，总学分      4
 * （5）	查询不及格学生信息：学号，姓名，专业名称，补考课程名称             4
 * （6）	查询奖学金学生信息（要求：无不及格记录，且平均分在85分以上）：学号，姓名，专业名称，平均分      4
 */
@Component
public class QueryPanel extends JPanel {
//    final private AdminService adminService = Main.ctx.getBean(AdminService.class);
    private final AdminService adminService;

    public QueryPanel(AdminService adminService) {
        this.adminService = adminService;
        initComponents();
        decoration();
    }

    public void decoration() {
        buttonStudentScore.addActionListener(e -> {
            labelMsg.setText("查询XXX同学各科成绩：学号，姓名，专业名称，课程名称，成绩");
            Vector<Vector<Object>> tableData = new Vector<>();
            Vector<String> tableTitle = new Vector<>();

            String text = textFieldQuery.getText();
            List<Map<String, Object>> maps = adminService.scoreByStudentIdOrName(text);
            for (Map<String, Object> map : maps) {
                Vector<Object> row = new Vector<>();
                row.add(map.get("studentId"));
                row.add(map.get("studentName"));
                row.add(map.get("majorName"));
                row.add(map.get("courseName"));
                row.add(map.get("score"));
                tableData.add(row);
            }

            // 	查询XXX同学各科成绩：学号，姓名，专业名称，课程名称，成绩
            tableTitle.add("学号");
            tableTitle.add("姓名");
            tableTitle.add("专业名称");
            tableTitle.add("课程名称");
            tableTitle.add("成绩");

            table1.setModel(new DefaultTableModel(tableData, tableTitle) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            });
        });

        buttonMajorNumber.addActionListener(e -> {
            labelMsg.setText("按专业统计学生人数：专业，学生人数");
            Vector<Vector<Object>> tableData = new Vector<>();
            Vector<String> tableTitle = new Vector<>();

            List<Map<String, Object>> maps = adminService.statisticStudentNumberByMajor();
            for (Map<String, Object> map : maps) {
                Vector<Object> row = new Vector<>();
                row.add(map.get("majorName"));
                row.add(map.get("number"));
                tableData.add(row);
            }

            // 按专业统计学生人数：专业，学生人数
            tableTitle.add("专业");
            tableTitle.add("学生人数");

            table1.setModel(new DefaultTableModel(tableData, tableTitle) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            });
        });

        buttonCourseScore.addActionListener(e -> {
            labelMsg.setText("查询某门课程成绩单：学号，姓名，专业名称，(课程名称)，成绩");
            Vector<Vector<Object>> tableData = new Vector<>();
            Vector<String> tableTitle = new Vector<>();

            String text = textFieldQuery.getText();
            List<Map<String, Object>> maps = adminService.scoreByCourseIdOrName(text);
            for (Map<String, Object> map : maps) {
                Vector<Object> row = new Vector<>();
                row.add(map.get("studentId"));
                row.add(map.get("studentName"));
                row.add(map.get("majorName"));
                row.add(map.get("courseName"));
                row.add(map.get("score"));
                tableData.add(row);
            }

            // 	查询某门课程成绩单：学号，姓名，专业名称，课程名称，成绩
            tableTitle.add("学号");
            tableTitle.add("姓名");
            tableTitle.add("专业名称");
            tableTitle.add("课程名称");
            tableTitle.add("成绩");

            table1.setModel(new DefaultTableModel(tableData, tableTitle) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            });
        });

        buttonTotalCredit.addActionListener(e -> {
            labelMsg.setText("查询学生总学分，不及格不计学分：学号，姓名，专业名称，总学分");
            Vector<Vector<Object>> tableData = new Vector<>();
            Vector<String> tableTitle = new Vector<>();

            List<Map<String, Object>> maps = adminService.totalCredit();
            for (Map<String, Object> map : maps) {
                Vector<Object> row = new Vector<>();
                row.add(map.get("studentId"));
                row.add(map.get("studentName"));
                row.add(map.get("majorName"));
                row.add(map.get("sumCredit"));
                tableData.add(row);
            }

            // 	查询学生总学分，不及格不计学分：学号，姓名，专业名称，总学分
            tableTitle.add("学号");
            tableTitle.add("姓名");
            tableTitle.add("专业名称");
            tableTitle.add("总学分");

            table1.setModel(new DefaultTableModel(tableData, tableTitle) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            });
        });

        buttonFail.addActionListener(e -> {
            labelMsg.setText("查询不及格学生信息：学号，姓名，专业名称，补考课程名称");
            Vector<Vector<Object>> tableData = new Vector<>();
            Vector<String> tableTitle = new Vector<>();

            List<Map<String, Object>> maps = adminService.failExam();
            for (Map<String, Object> map : maps) {
                Vector<Object> row = new Vector<>();
                row.add(map.get("studentId"));
                row.add(map.get("studentName"));
                row.add(map.get("majorName"));
                row.add(map.get("courseName"));
                tableData.add(row);
            }

            // 查询不及格学生信息：学号，姓名，专业名称，补考课程名称
            tableTitle.add("学号");
            tableTitle.add("姓名");
            tableTitle.add("专业名称");
            tableTitle.add("补考课程名称");

            table1.setModel(new DefaultTableModel(tableData, tableTitle) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            });
        });

        buttonScholarship.addActionListener(e -> {
            labelMsg.setText("查询奖学金学生信息（要求：无不及格记录，且平均分在85分以上）：学号，姓名，专业名称，平均分");
            Vector<Vector<Object>> tableData = new Vector<>();
            Vector<String> tableTitle = new Vector<>();

            List<Map<String, Object>> maps = adminService.gainScholarship();
            for (Map<String, Object> map : maps) {
                Vector<Object> row = new Vector<>();
                row.add(map.get("studentId"));
                row.add(map.get("studentName"));
                row.add(map.get("majorName"));
                row.add(map.get("avgScore"));
                tableData.add(row);
            }

            // 查询奖学金学生信息（要求：无不及格记录，且平均分在85分以上）：学号，姓名，专业名称，平均分
            tableTitle.add("学号");
            tableTitle.add("姓名");
            tableTitle.add("专业名称");
            tableTitle.add("平均分");

            table1.setModel(new DefaultTableModel(tableData, tableTitle) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            });
        });
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        ResourceBundle bundle = ResourceBundle.getBundle("swing_text");
        textFieldQuery = new JTextField();
        buttonStudentScore = new JButton();
        buttonMajorNumber = new JButton();
        buttonCourseScore = new JButton();
        buttonTotalCredit = new JButton();
        buttonFail = new JButton();
        buttonScholarship = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        labelMsg = new JLabel();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {25, 105, 105, 105, 105, 105, 105, 105, 105, 20, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {25, 25, 305, 20, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};
        add(textFieldQuery, new GridBagConstraints(1, 1, 2, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- buttonStudentScore ----
        buttonStudentScore.setText(bundle.getString("QueryPanel.buttonStudentScore.text"));
        add(buttonStudentScore, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- buttonMajorNumber ----
        buttonMajorNumber.setText(bundle.getString("QueryPanel.buttonMajorNumber.text"));
        add(buttonMajorNumber, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- buttonCourseScore ----
        buttonCourseScore.setText(bundle.getString("QueryPanel.buttonCourseScore.text"));
        add(buttonCourseScore, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- buttonTotalCredit ----
        buttonTotalCredit.setText(bundle.getString("QueryPanel.buttonTotalCredit.text"));
        add(buttonTotalCredit, new GridBagConstraints(6, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- buttonFail ----
        buttonFail.setText(bundle.getString("QueryPanel.buttonFail.text"));
        add(buttonFail, new GridBagConstraints(7, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- buttonScholarship ----
        buttonScholarship.setText(bundle.getString("QueryPanel.buttonScholarship.text"));
        add(buttonScholarship, new GridBagConstraints(8, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1, new GridBagConstraints(1, 2, 8, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));

        //---- labelMsg ----
        labelMsg.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 12));
        add(labelMsg, new GridBagConstraints(2, 3, 6, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 5), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTextField textFieldQuery;
    private JButton buttonStudentScore;
    private JButton buttonMajorNumber;
    private JButton buttonCourseScore;
    private JButton buttonTotalCredit;
    private JButton buttonFail;
    private JButton buttonScholarship;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel labelMsg;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
